<template>
  <div class="container">
    <el-card class="box-card">
      <div slot="header" v-if="queryId">
        <!-- 头部面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>大数据</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 搜素框 -->
      <el-form label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="目录名称" label-width="80px">
              <el-input v-model.trim="queryInfo.directoryName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select
                v-model="queryInfo.state"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button @click="resetSelect">清除</el-button>
            <el-button type="primary" @click="getDirectoryList">搜索</el-button>
          </el-col>
          <el-col :span="6">
            <el-row type="flex" justify="end">
              <el-button
                type="text"
                icon="el-icon-back"
                v-if="queryId"
                @click="$router.back()"
                >返回学科</el-button
              >
              <el-button
                type="success"
                icon="el-icon-edit"
                @click="showAddDirectoryDialog"
                >新增目录</el-button
              >
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <!-- 警告 -->
      <el-alert type="info" show-icon>数据一共{{ total }}条</el-alert>
      <!-- 表格 -->
      <el-table :data="directoryList">
        <el-table-column
          type="index"
          label="序号"
          width="100"
        ></el-table-column>
        <el-table-column prop="subjectName" label="所属学科"></el-table-column>
        <el-table-column
          prop="directoryName"
          label="目录名称"
        ></el-table-column>
        <el-table-column prop="creatorID" label="创建者"></el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="scope">
            {{
              scope.row.addDate | parseTimeByString("{y}-{m}-{d} {h}:{i}:{s}")
            }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"></el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            {{ scope.row.state === 1 ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="changeState(scope.row)">
              {{ scope.row.state === 1 ? "禁用" : "启用" }}
            </el-button>
            <el-button
              type="text"
              :disabled="scope.row.state === 1 ? true : false"
              @click="editDirectory(scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="scope.row.state === 1 ? true : false"
              @click="deleteDirectory(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row class="Pagination" type="flex" justify="end">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[10, 15, 20, 25]"
          :page-size="queryInfo.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 新增目录弹层 -->
    <el-dialog
      :title="addDirectoryForm.id ? '修改目录' : '新增目录'"
      :visible.sync="showAddDialog"
      width="30%"
    >
      <el-form
        :rules="rules"
        :model="addDirectoryForm"
        label-width="100px"
        ref="addDirectoryFormRef"
      >
        <el-form-item label="所属学科" v-if="!queryId">
          <el-select
            v-model="addDirectoryForm.subjectID"
            style="width: 89%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in SimpleSubjectList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            v-model="addDirectoryForm.directoryName"
            placeholder="请输入目录名称"
            style="width: 89%"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, changeState, remove, update, add, detail } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
import { status } from '@/api/hmmm/constants.js'
export default {
  name: 'Directorys',
  data () {
    return {
      // 目录列表请求对象
      queryInfo: {
        page: 1, // 页码值
        pagesize: 10, // 页容量
        directoryName: null, // 目录名称
        state: null // 下拉选中状态0,1
      },
      total: 0, // 目录总条数
      directoryList: [], // 目录列表
      status,
      showAddDialog: false, // 控制新增目录弹层
      // 添加目录数据
      addDirectoryForm: {
        subjectID: null,
        directoryName: null
      },
      rules: {
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      },
      SimpleSubjectList: [], // 学科简单列表
      queryId: this.$route.query.id
    }
  },
  created () {
    this.getDirectoryList()
    this.getSimpleList()
  },
  methods: {
    // 获取目录列表-------------------------------------------------------
    async getDirectoryList () {
      const { data } = await list(this.queryInfo)
      // console.log(data)
      this.directoryList = data.items
      this.total = data.counts
    },
    // 当前页容量变化触发----------------------------------------------------
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getDirectoryList()
    },
    // 当前页码值变化触发------------------------------------------------------
    handleCurrentChange (val) {
      this.queryInfo.page = val
      this.getDirectoryList()
    },
    // 重置搜索框-------------------------------------------------------------
    resetSelect () {
      this.queryInfo.directoryName = null
      this.queryInfo.state = null
      this.getDirectoryList()
    },
    // 点击新增目录触发----------------------------------------------------------
    showAddDirectoryDialog () {
      this.showAddDialog = true
    },
    // 取消按钮----------------------------------------------------------------
    btnCancel () {
      this.showAddDialog = false
      this.$refs.addDirectoryFormRef.resetFields()
      this.addDirectoryForm.subjectID = null
      if (this.addDirectoryForm.id) {
        this.addDirectoryForm.id = null
      }
    },
    // 确定按钮-------------------------------------------------------------
    async btnOK () {
      await this.$refs.addDirectoryFormRef.validate()
      if (this.queryId) {
        if (this.addDirectoryForm.id) {
          await update(this.addDirectoryForm)
          this.$message.success('修改目录成功')
        } else {
          await add({ subjectID: Number(this.queryId), directoryName: this.addDirectoryForm.directoryName })
          this.$message.success('新增目录成功')
        }
      } else {
        if (this.addDirectoryForm.id) {
          await update(this.addDirectoryForm)
          this.$message.success('修改目录成功')
        } else {
          await add(this.addDirectoryForm)
          this.$message.success('新增目录成功')
        }
      }
      this.getDirectoryList()
      this.showAddDialog = false
      this.$refs.addDirectoryFormRef.resetFields()
      this.addDirectoryForm.id = null
    },
    // 改变目录状态--------------------------------------------------------------
    async changeState (row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.getDirectoryList()
    },
    // 删除目录--------------------------------------------------------------
    deleteDirectory (id) {
      this.$confirm('此操作将永久删除该目录,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await remove(id)
        this.$message.success('删除成功')
        this.getDirectoryList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 获取目录详情---------------------------------------------------------------------
    async editDirectory (id) {
      this.showAddDialog = true
      const result = await detail(id)
      // console.log(result.data)
      this.addDirectoryForm = result.data
    },
    // 获取学科简单列表----------------------------------------------------------
    async getSimpleList () {
      const { data } = await simple()
      // console.log(data)
      this.SimpleSubjectList = data
    }
  }
}
</script>

<style scoped lang='scss'>
.el-alert {
  margin-bottom: 10px;
}
.Pagination {
  margin-top: 10px;
}
</style>
